/*
 * @Author: Shandong Xiedali
 * @Date: 2022-04-10 23:30:52
 * @LastEditors  : Shandong Xiedali
 * @LastEditTime : 2022-04-20 12:11:56
 * @FilePath     : NumberHelper.ts
 * @Description:
 *
 * Copyright (c) 2022 by Hiland/RainyTop, All Rights Reserved.
 */

import {StringHelper} from "./stringHelper";
import {Convertor}    from "./convertor";
import {ArrayHelper}  from "./arrayHelper";

export class NumberHelper {
    /**
     * 获取带精度的浮点数
     * @param {number|string} value 待计算数值
     * @param {number} precision  获取结果的精度(默认2位小数精度)
     * @returns {number|string}
     */
    static getFloat(value: number | string, precision: number = 2): string {
        const paddingZeros = StringHelper.multi("0", precision);

        const calcMedia = parseInt("1" + paddingZeros);
        value = Math.round(parseFloat(typeof value === "string" ? value : value.toString()) * calcMedia) / calcMedia;
        let valuePartsSeparateDot = value.toString().split(".");

        if (valuePartsSeparateDot.length === 1) {
            value = value.toString();
            if (paddingZeros != "") {
                value = `${value}.${paddingZeros}`;
            }
            return value;
        }

        // 目前小数点后面的位数
        const existPrecision: number = valuePartsSeparateDot[1].length;
        const stillPaddingZeros = StringHelper.multi("0", precision - existPrecision);
        value = value.toString() + stillPaddingZeros;

        return value.toString();
    }

    /**
     * 获取平均值
     * @param {number[]} numbers 待计算数值数组
     * @returns {number} 平均值
     */
    static getAverage(...numbers: number[]): number {
        return numbers.reduce((acc, val) => acc + val, 0) / numbers.length;
    }

    /**
     * 数学中各种进制之间的相互转换,返回字符串表示的数据
     * @param string numberString 待转换的(字符串格式的)目标数字
     * @param int fromBase 从**进制转换
     * @param int toBase 转换到**进制
     * @return string
     */
    static convertBase(numberString: string, fromBase: number, toBase: number): string {
        return Convertor.convertBase(numberString, fromBase, toBase);
    }

    /**
     * @desc: 计算移动平均值
     * @param {number[]} arrayData 待计算的数组
     * @param {number} windowSize 窗口大小
     * @return {number[]} 移动平均值数组
     */
    static getMovingAverage(arrayData: number[], windowSize: number): number[] {
        return ArrayHelper.getMovingAverage(arrayData, windowSize);
    }
}
